package com.todo.pro.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.todo.pro.entity.SystemLoginPic;
import com.todo.pro.service.SystemLoginPicService;
import com.todo.pro.shiro.SubjectUtil;

/**
 * @author SiZhongxia
 * @since 2017-12-27
 */
@Controller
@RequestMapping
public class IndexController extends BaseController {

	@Value("${system.name}")
	private String systemName;
	@Value("${system.version}")
	private String systemVersion;
	@Value("${system.copyright}")
	private String systemCopyRight;
	@Value("${system.logo}")
	private String systemLogo;

	@Autowired
	private SystemLoginPicService systemLoginPicService;

	@RequiresAuthentication
	@RequestMapping(value = { "index" }, method = { RequestMethod.GET })
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("index");
		mav.addObject("systemName", systemName);
		mav.addObject("systemVersion", systemVersion);
		mav.addObject("systemCopyRight", systemCopyRight);
		return mav;
	}

	@RequestMapping(value = "login")
	public ModelAndView login(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		if (SubjectUtil.getSubject().isAuthenticated()) {
			mav.setViewName("redirect:/index");
			return mav;
		}
		mav.setViewName("login");
		mav.addObject("systemName", systemName);
		mav.addObject("systemVersion", systemVersion);
		mav.addObject("systemCopyRight", systemCopyRight);

		Wrapper<SystemLoginPic> systemLoginPicWrapper = new EntityWrapper<>();
		systemLoginPicWrapper.eq("used", 1);
		systemLoginPicWrapper.orderBy("update_time", false);
		SystemLoginPic systemLoginPic = systemLoginPicService.selectOne(systemLoginPicWrapper);
		if(systemLoginPic != null) {
			mav.addObject("loginPagePicTitle", systemLoginPic.getTitle());
			mav.addObject("loginPagePicUrl", systemLoginPic.getUrl());
		} else {
			mav.addObject("loginPagePicTitle", "bg_login");
			mav.addObject("loginPagePicUrl", "./statics/assets/images/bg_login.png");
		}
		return mav;
	}

	@RequiresAuthentication
	@RequestMapping(value = "header")
	public ModelAndView header(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("components/header");
		mav.addObject("systemName", systemName);
		mav.addObject("systemLogo", systemLogo);
		return mav;
	}

	@RequiresAuthentication
	@RequestMapping(value = "sidenav")
	public ModelAndView sidenav(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("components/sidenav");
		return mav;
	}

	@RequiresAuthentication
	@RequestMapping(value = "console")
	public ModelAndView console(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("components/console");
		return mav;
	}

	@RequiresAuthentication
	@RequestMapping(value = "test")
	public ModelAndView test(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("components/test");
		return mav;
	}
	
	@RequiresAuthentication
	@RequestMapping(value = "logout")
	public ModelAndView logout(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("redirect:/login");
		Subject subject = SecurityUtils.getSubject();
		if (subject.isAuthenticated()) {
			subject.logout();
		}
		return mav;
	}

	@RequestMapping(value = "pagenotfound")
	public ModelAndView pagenotfound(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("error/pagenotfound");
		return mav;
	}

	@RequestMapping(value = "serverbusy")
	public ModelAndView serverbusy(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("error/serverbusy");
		return mav;
	}

}
